package leetcodev1.链表;

public class LeetCode2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode ret = new ListNode(-1), index = ret;
        int next = 0;
        while (l1 != null || l2 != null) {
            int num1 = l1 != null ? l1.val : 0;
            int num2 = l2 != null ? l2.val : 0;
            index.next = new ListNode((num1 + num2 + next) % 10);
            next = (num1 + num2 + next) >= 10 ? 1 : 0;

            if (l1 != null) {
                l1 = l1.next;
            }

            if (l2 != null) {
                l2 = l2.next;
            }
            index = index.next;
        }
        if (next != 0) {
            index.next = new ListNode(next);
        }
        return ret.next;
    }
}
